package com.tamako.maaremote.conf;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.UUID;


/**
 * @author Tamako
 */
@Slf4j
@RequiredArgsConstructor
public class LoginAuthInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String method = request.getMethod();
        // 如果是跨域预检请求，直接放行
        if ("OPTIONS".equals(method) || request.getRequestURI().contains("/error")) {
            return true;
        }
        AuthContextUtil.set(UUID.randomUUID().toString());
        return true;

    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        AuthContextUtil.remove();  // 移除threadLocal中的数据
    }

}